package com.youdianstar.app.adapter

import android.content.Context
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.recyclerview.widget.RecyclerView
import com.youdianstar.app.databinding.LayoutItemPlacesBinding
import com.youdianstar.app.entity.PlacesEntity
import com.youdianstar.app.support.imp.IPlacesSelectListener


/**
 * File: PlacesListAdapter
 * Author: 82149 Create: 2023/7/14 11:14
 * Changes (from 2023/7/14)
 * --------------------------------------------------
 * description: 地图地址反编译 列表
 * ---------------------------------------------------
 *
 */
class PlacesListAdapter(val context: Context,
                        val dataList: MutableList<PlacesEntity>,val listener:IPlacesSelectListener)
    : RecyclerView.Adapter<PlacesListAdapter.ListHolder>(){

    class ListHolder(val binding : LayoutItemPlacesBinding) : RecyclerView.ViewHolder(binding.root)

    override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ListHolder {
        return ListHolder(LayoutItemPlacesBinding.inflate(LayoutInflater.from(parent.context), parent, false))
    }

    override fun onBindViewHolder(holder: ListHolder, position: Int) {
        dataList[position].let { item ->
            holder.binding.ivStatus.visibility = if (item.isSelected) View.VISIBLE else View.INVISIBLE
            holder.binding.nameText.text = item.name
            holder.binding.addressText.text = item.address
        }
    }

    override fun getItemCount(): Int {
        return dataList.size
    }

    fun updateList(list:MutableList<PlacesEntity>){
        dataList.clear()
        if (list.isNotEmpty()){
            dataList.addAll(list)
        }
        this.notifyDataSetChanged()
    }

    fun updateItemSelect(position: Int){
        if (dataList.isNotEmpty()){
            dataList.forEach { item ->
                item.isSelected = false
            }
            dataList[position].isSelected = true
        }
        this.notifyDataSetChanged()
    }
}